<?php
class MarcasController extends AppController {

	var $name = 'Marcas';
	var $helpers = array('Html', 'Form');

	function index() {
		$this->Marca->recursive = 0;
		$this->set('marcas', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Marca', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('marca', $this->Marca->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Marca->create();
			if ($this->Marca->save($this->data)) {
				$this->Session->setFlash(__('The Marca has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Marca could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Marca', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Marca->save($this->data)) {
				$this->Session->setFlash(__('The Marca has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Marca could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Marca->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Marca', true));
			$this->redirect(array('action' => 'index'));
		}
		if ($this->Marca->del($id)) {
			$this->Session->setFlash(__('Marca deleted', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('The Marca could not be deleted. Please, try again.', true));
		$this->redirect(array('action' => 'index'));
	}


	function admin_index() {
		$this->Marca->recursive = 0;
		$this->set('marcas', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Marca', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('marca', $this->Marca->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Marca->create();
			if ($this->Marca->save($this->data)) {
				$this->Session->setFlash(__('The Marca has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Marca could not be saved. Please, try again.', true));
			}
		}
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Marca', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Marca->save($this->data)) {
				$this->Session->setFlash(__('The Marca has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Marca could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Marca->read(null, $id);
		}
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Marca', true));
			$this->redirect(array('action' => 'index'));
		}
		if ($this->Marca->del($id)) {
			$this->Session->setFlash(__('Marca deleted', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('The Marca could not be deleted. Please, try again.', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function listaMarcasAjax()
	{
		Configure::write('debug',0);
		
		$marcasMatch = $this->Marca->query("select id, nombre from marcas as Marca where nombre like '%".$_GET['q']."%'");
		
		$content  = "";
		
		for($x=0; $x < count($marcasMatch) ; $x++)
		{
			$content  .= trim($marcasMatch[$x]['Marca']['nombre'])."|".trim($marcasMatch[$x]['Marca']['id'])."\n";
		}
		
		
		
		$this->layout = "ajax";
		$this->set('content',$content);
	}

	

}
?>